Ext.define('Xyh.controller.Users', {
	extend : 'Ext.app.Controller',
	models : ['User'],
	stores : ['Users'],
	views : ['user.Login', 'user.Main', 'user.List', 'user.Add', 'user.Edit','report.Month',
			'user.RoleConfig'],

	init : function() {
		this.control({
					'login button[action=login]' : {
						click : this.login
					},
					'user button[action=adduser]' : {
						click : this.showAddUser
					},
					'useradd button[action=save]' : {
						click : this.addUser
					},
					'user' : {
						itemdblclick : this.showUpdateWindow
					},
					'useredit button[action=save]' : {
						click : this.updateUser
					},
					'roleconfig button[action=save]' : {
						click : this.roleConfig
					}
				});

	},
	showAddUser : function(button) {
		Ext.widget('useradd');
	},
	addUser : function(button) {
		var win = button.up('window');
		var form = win.down('form').getForm();
		if (form.isValid()) {
			form.submit({
						success : function(form, action) {
							Ext.Msg.alert('OK', '添加成功');
							win.close();
							Ext.getStore('Users').load();

						},
						failure : function(form, action) {
							Ext.Msg.alert('ERROR', '添加失败,可能原因是用户名已存在,请检查后再添加');
						}
					});
		}
	},
	showUpdateWindow : function(grid, record) {
		var edit = Ext.createWidget('useredit');
		edit.down('form').loadRecord(record);

	},
	updateUser : function(button) {
		var win = button.up('window');
		var form = win.down('form').getForm();
		if (form.isValid()) {
			form.submit({
						success : function(form, action) {
							Ext.Msg.alert('OK', '修改成功');
							win.close();
							Ext.getStore('Users').load();
						},
						failure : function(form, action) {
							Ext.Msg.alert('ERROR', '修改失败');
						}
					});
		}
	},
	login : function(button) {
		var form = button.up('form').getForm();
		if (form.isValid()) {
			form.submit({
				waitMsg : '正在验证用户名和密码...',
				success : function() {
					var _view = Ext.getCmp("viewport_main");
					_view.removeAll();

					var _main = Ext.create('widget.main');
					_view.add(_main);
					var menuPanel = Ext.getCmp("main_menu");
					Ext.Ajax.request({
						url : '/menu/all/',
						success : function(response) {
							var menuTypes = Ext.decode(response.responseText);
							var controllerMenu = globalApp
									.getController('Menu');
							Ext.each(menuTypes, function(menuType) {
								var menuConfig = {}
								menuConfig.title = menuType.name
								menuConfig.rootVisible = false;
								menuConfig.listeners = {
									itemclick : {
										fn : function(node, record) {
											controllerMenu['menuClick'](record);
										}
									}
								}
								menuConfig.root = {
									expanded : true,
									children : []
								};
								Ext.each(menuType.children, function(menu) {
									menuConfig.root.children.push({
												text : menu.name,
												leaf : true,
												functionName : menu.operation_function
											});
								});
								var subMenuPanel = Ext.create('Ext.tree.Panel',
										menuConfig);
								menuPanel.add(subMenuPanel);
							});
						}
					});
				},
				failure : function() {
					Ext.Msg.show({
								icon : Ext.Msg.ERROR,
								title : '出错了！',
								msg : '用户名或密码错误，请重新输入！'

							});
				}
			});
		}

	},
	roleConfig : function(button) {
		var win = button.up('window');
		var form = win.down('form').getForm();
		if (form.isValid()) {
			form.submit( {
				success : function(form, action) {
					Ext.Msg.alert('OK', '修改成功成功');
					win.close();

				},
				failure : function(form, action) {
					Ext.Msg.alert('ERROR', '修改失败');
				}
			});
		}
	}

});